package com.zmn.mcc.services.impl.staff.staffarchives;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.common.constant.StaffArchivesConsts;
import com.zmn.mcc.model.entity.staff.dingtalk.StaffDingTalk;
import com.zmn.mcc.model.entity.staff.staffarchives.StaffArchives;
import com.zmn.mcc.model.entity.staff.staffarchives.StaffArchivesQuery;
import com.zmn.mcc.persistence.interfaces.staff.staffarchives.StaffArchivesDAO;
import com.zmn.mcc.services.interfaces.staff.staffarchives.StaffArchivesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 描述: 员工档案基础接口实现类
 *
 * @author tanghongjun
 * @since 2020-05-14 10:21:58
 **/
@Slf4j
@Service
public class StaffArchivesServiceImpl implements StaffArchivesService {

    private static final String TAG = "员工档案基础服务接口";

    @Resource
    private StaffArchivesDAO staffArchivesDao;

    /**
     * 描述: 根据id查询数据
     *
     * @param archivesId id
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public StaffArchives findById(Integer archivesId) {

        if (Objects.isNull(archivesId)) {
            log.error("[{}],根据id查询数据archivesId为null", TAG);
            return null;
        }
        return staffArchivesDao.findById(archivesId);
    }

    @Override
    public StaffArchives findByStaffId(Integer staffId) {
        if (Objects.isNull(staffId)) {
            log.error("[{}],根据id查询数据staffId为null", TAG);
            return null;
        }
        return staffArchivesDao.findByStaffId(staffId);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public List<StaffArchives> listPageByQuery(StaffArchivesQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return staffArchivesDao.listPageByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public List<StaffArchives> listByQuery(StaffArchivesQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return staffArchivesDao.listByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer countByQuery(StaffArchivesQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return staffArchivesDao.countByQuery(query);
    }


    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param staffArchives 待修改数据
     * @return java.lang.Integer
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer updateStaffArchivesByKey(StaffArchives staffArchives) {

        if (Objects.isNull(staffArchives.getArchivesId())) {
            log.error("[{}],根据id修改数据,staffArchives为null", TAG);
            return null;
        }
        return staffArchivesDao.updateStaffArchivesByKey(staffArchives);
    }

    @Override
    public Integer synchroByDingTalk(Integer staffId, StaffDingTalk staffDingTalk) {
        if (Objects.isNull(staffId)) {
            log.error("员工ID不能为空", TAG);
            return null;
        }
        StaffArchives staffArchives = staffArchivesDao.findByStaffId(staffId);
        if(Objects.isNull(staffArchives)){
            staffArchives = new StaffArchives();
            staffArchives.setStaffId(staffId);
        }
        if(StringUtils.isEmpty(staffArchives.getIdCardNumber()) && Objects.nonNull(staffDingTalk.getIdCardNumber())){
            staffArchives.setIdCardNumber(staffDingTalk.getIdCardNumber());
        }
        if(Objects.isNull(staffArchives.getBirthday()) && Objects.nonNull(staffDingTalk.getBirthday())){
            staffArchives.setBirthday(staffDingTalk.getBirthday());
        }
        if(Objects.isNull(staffArchives.getSexType()) && Objects.nonNull(staffDingTalk.getSexType())){
            staffArchives.setSexType(staffDingTalk.getSexType());
        }
        if(StringUtils.isEmpty(staffArchives.getNationType()) && Objects.nonNull(staffDingTalk.getNationType())){
            staffArchives.setNationType(staffDingTalk.getNationType());
        }
        if(StringUtils.isEmpty(staffArchives.getIdCardAdress()) && Objects.nonNull(staffDingTalk.getIdCardAdress())){
            staffArchives.setIdCardAdress(staffDingTalk.getIdCardAdress());
        }
        if(Objects.isNull(staffArchives.getIdCardValidityTime()) && Objects.nonNull(staffDingTalk.getIdCardValidityTime())){
            staffArchives.setIdCardValidityTime(staffDingTalk.getIdCardValidityTime());
            staffArchives.setIdCardValidityType(StaffArchivesConsts.ID_CARD_VALIDIT_TYPE_TIME_LIMIT);
        }
        if(Objects.isNull(staffArchives.getMarriageStatus()) && Objects.nonNull(staffDingTalk.getMarriageStatus())){
            staffArchives.setMarriageStatus(staffDingTalk.getMarriageStatus());
        }
        if(Objects.isNull(staffArchives.getJoinWorkingTime()) && Objects.nonNull(staffDingTalk.getJoinWorkingTime())){
            staffArchives.setJoinWorkingTime(staffDingTalk.getJoinWorkingTime());
        }
        if(Objects.isNull(staffArchives.getResidenceType()) && Objects.nonNull(staffDingTalk.getResidenceType())){
            staffArchives.setResidenceType(staffDingTalk.getResidenceType());
        }
        if(StringUtils.isEmpty(staffArchives.getAddress()) && Objects.nonNull(staffDingTalk.getRegisteredAdress())){
            staffArchives.setAddress(staffDingTalk.getRegisteredAdress());
        }
        if(Objects.isNull(staffArchives.getPoliticalStatus()) && Objects.nonNull(staffDingTalk.getPoliticalStatus())){
            staffArchives.setPoliticalStatus(staffDingTalk.getPoliticalStatus());
        }
        if(Objects.isNull(staffArchives.getHighestEdu()) && Objects.nonNull(staffDingTalk.getHighestEdu())){
            staffArchives.setHighestEdu(staffDingTalk.getHighestEdu());
        }
        if(StringUtils.isEmpty(staffArchives.getGraduateSchool()) && Objects.nonNull(staffDingTalk.getGraduateSchool())){
            staffArchives.setGraduateSchool(staffDingTalk.getGraduateSchool());
        }
        if(Objects.isNull(staffArchives.getGraduationTime()) && Objects.nonNull(staffDingTalk.getGraduationTime())){
            staffArchives.setGraduationTime(staffDingTalk.getGraduationTime());
        }
        if(StringUtils.isEmpty(staffArchives.getMajor()) && Objects.nonNull(staffDingTalk.getMajor())){
            staffArchives.setMajor(staffDingTalk.getMajor());
        }
        if(StringUtils.isEmpty(staffArchives.getBankCardNoVarchar()) && Objects.nonNull(staffDingTalk.getBankCardNo())){
            staffArchives.setBankCardNoVarchar(staffDingTalk.getBankCardNo());
        }
        if(NumberUtil.isNullOrZero(staffArchives.getArchivesId())){
            return staffArchivesDao.insert(staffArchives);
        }
        return staffArchivesDao.updateStaffArchivesByKey(staffArchives);
    }

    /**
     * 描述: 新增数据
     *
     * @param staffArchives 待新增数据
     * @return java.lang.Integer 主键key
     * @author tanghongjun
     * @since 2020-05-14 10:21:58
     **/
    @Override
    public Integer insert(StaffArchives staffArchives) {

        if (Objects.isNull(staffArchives)) {
            log.error("[{}],新增数据,staffArchives为null", TAG);
            return null;
        }
        return staffArchivesDao.insert(staffArchives);
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param archivesId
     * @return: 执行结果码
     * @auther: tanghongjun
     * @since: 2020-05-14 10:21:58
     */
    @Override
    public Integer deleteByKey(Integer archivesId) {

        if (Objects.isNull(archivesId)) {
            log.error("[{}],根据id删除数据,archivesId为null", TAG);
            return null;
        }
        return staffArchivesDao.deleteByKey(archivesId);
    }

}